package com.qf.shop.common.pool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 项目中的线程池
 */
public class ShopExcutors {

    // 1.创建一个自己的线程池
    private static ThreadPoolExecutor executors = null;

    static {
        System.out.println("ShopExcutors.static initializer");
        executors = new
                ThreadPoolExecutor(
                5, // 核心线程池,线程池中至少有5个线程
                15, // 线程池中的最大线程数量
                3,  // 空闲线程回收的时间
                TimeUnit.SECONDS, // 时间单位
                new ArrayBlockingQueue(20), // 线程池中的线程已经最大了，剩下的引用压入队列等待空闲线程
                Executors.defaultThreadFactory(),  // 创建线程的默认工厂
                new ThreadPoolExecutor.AbortPolicy() // 拒绝策略，队列的长度满了以后执行的拒绝策略
        );
    }

    public static ThreadPoolExecutor getExecutor() {
        return executors;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                ThreadPoolExecutor executor = ShopExcutors.getExecutor();
                System.out.println(executor);
            }).start();
        }
    }
}
